package jl.cts.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;

public class Sprite {

	private int startX;
	private int startY;
	private int x;
	private int y;
	private int pivotX;
	private int pivotY;
	private int width;
	private int height;
	private Bitmap image = null;
	private boolean visible;
	private int hdirection;// 0 = Left 1 = Right
	private int vdirection;// 0 = Top 1 = Bottom
	private int speedX, speedY;
	private Matrix matrix = null;
	private float rotation = 0;

	private Bitmap imgDisabled = null;
	private Bitmap imgEnabled = null;
	private boolean isEnabled = true;
	private Context context;
	private boolean isMoving;

	public Sprite(Context context, Integer resourceId, int x, int y) {
		this.context = context;
		image = Util.decodeBitmap(context.getResources(), resourceId);
		this.x = x;
		this.y = y;
		this.startX = x;
		this.startY = y;
		this.width = image.getWidth();
		this.height = image.getHeight();
		this.imgEnabled = image;
		this.visible = true;
		this.isMoving = false;
		this.setHdirection(0);
		this.setVdirection(0);
		this.matrix = new Matrix();
		this.pivotX = width / 2;
		this.pivotY = height / 2;
	}

	public void setDisabled() {
		isEnabled = false;
		this.image = imgDisabled;
	}

	public void setEnabled() {
		isEnabled = true;
		this.image = imgEnabled;
	}

	public Rect getBounds() {
		Rect r = new Rect();
		r.top = (int) this.y;
		r.left = (int) this.x;
		r.bottom = (int) (this.y + this.height);
		r.right = (int) (this.x + this.width);
		return r;
	}

	public boolean collidesInPoint(float x, float y) {
		Rect rect = new Rect((int) x, (int) y, (int) x + 1, (int) y + 1);
		if (this.getBounds().intersect(rect))
			return true;
		return false;
	}

	public boolean isColliding(Sprite s) {
		if (this.getBounds().intersect(s.getBounds()))
			return true;
		return false;
	}

	public void setPosition(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public void render(Canvas canvas, Paint paint) {
		if (visible) {

			if (isMoving) {
				move();
			}

			matrix.reset();
			matrix.postTranslate(x, y);
			matrix.postRotate(rotation, x + pivotX, y + pivotY);
			canvas.drawBitmap(getImage(), matrix, paint);

		}
	}

	

	public void setSpeed(int speedX, int speedY) {
			this.speedX = speedX;
			this.speedY = speedY;
	}

	public void move() {
		x = x + speedX;
		y = y + speedY;
	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getHdirection() {
		return hdirection;
	}

	public void setHdirection(int hdirection) {
		this.hdirection = hdirection;
	}

	public int getVdirection() {
		return vdirection;
	}

	public void setVdirection(int vdirection) {
		this.vdirection = vdirection;
	}

	public void setRefPosition(int x, int y) {
		this.x = x - this.width / 2;
		this.y = y - this.height / 2;
	}

	public boolean isCollide(Rect r) {
		if (this.getBounds().intersect(r))
			return true;
		return false;
	}

	public int getStartX() {
		return startX;
	}

	public void setStartX(int startX) {
		this.startX = startX;
	}

	public int getStartY() {
		return startY;
	}

	public void setStartY(int startY) {
		this.startY = startY;
	}

	public void checkTowardSpeed() {

	}

	public int getSpeedX() {
		return speedX;
	}

	public void setSpeedX(int speedX) {
		this.speedX = speedX;
	}

	public int getSpeedY() {
		return speedY;
	}

	public void setSpeedY(int speedY) {
		this.speedY = speedY;
	}

	public float getRotation() {
		return rotation;
	}

	public void setRotation(float rotation) {
		this.rotation = rotation;
	}

	public Bitmap getImage() {
		if (isEnabled) {
			return image;
		} else {
			return imgDisabled;
		}
	}

	public void setImage(Integer resourceId) {
		this.image = Util.decodeBitmap(context.getResources(), resourceId);
		this.setWidth(image.getWidth());
		this.setHeight(image.getHeight());
	}

	public void setImgDisabled(Bitmap imgDisabled) {
		this.imgDisabled = imgDisabled;
	}

	public Bitmap getImgDisabled() {
		return imgDisabled;
	}

	public boolean isEnabled() {
		return isEnabled;
	}

	public void unloadResources() {
		if (imgDisabled != null) {
			imgDisabled.recycle();
			imgDisabled = null;
		}

		if (imgEnabled != null) {
			imgEnabled.recycle();
			imgEnabled = null;
		}

		if (image != null) {
			image.recycle();
			image = null;
		}

	}

	public int getPivotX() {
		return pivotX;
	}

	public void setPivotX(int pivotX) {
		this.pivotX = pivotX;
	}

	public int getPivotY() {
		return pivotY;
	}

	public void setPivotY(int pivotY) {
		this.pivotY = pivotY;
	}

	public boolean isMoving() {
		return isMoving;
	}

	public void setMoving(boolean isMoving) {
		this.isMoving = isMoving;
	}

}
